package com.jlc.book.shop.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jlc.book.shop.delegate.UserDelegate;
import com.jlc.book.shop.to.UserTO;
import com.jlc.book.shop.validator.JLCDataValidator;

public class LoginAction {

	public String verifyUser(HttpServletRequest request,
			HttpServletResponse response) {
		
		String page="index.jsp";
		String uname=request.getParameter("uname");
		String pass=request.getParameter("pass");
		
		boolean intractWithDB=true;
		
		if(!JLCDataValidator.validateRequired(uname))
		{
			request.setAttribute("uname", "Username is required");
			intractWithDB=false;
		}
		
		if(!JLCDataValidator.validateRequired(pass))
		{
			request.setAttribute("pass", "Password is required");
			intractWithDB=false;
		}
		if(intractWithDB)
		{
			UserTO uto=UserDelegate.verifyUser(uname,pass);
			if(uto!=null)
			{
				request.getSession().setAttribute("USER_TO", uto);
				page="userHomeDef.jsp";
			}
			else
			{
				request.setAttribute("loginError", "Username or password is invalid");
			}
		}
		
		
		
		return page;
	}

}
